package com.xframe.domain.repository;

import com.xframe.domain.BaseModel;
import com.xframe.domain.role.IUserCredentials;
import com.xframe.xdal.core.constant.ErrorCode;
import com.xframe.xdal.core.domain.BaseRepository;
import com.xframe.xdal.core.domain.XDynamicModel;
import com.xframe.xdal.core.exception.DalFrameException;
import com.xframe.xdal.core.model.*;
import com.xframe.xdal.core.model.mapping.IColumnMapping;
import com.xframe.xdal.core.model.mapping.ITableInfo;
import com.xframe.xdal.core.util.StringUtil;
import com.xframe.xdal.core.util.XReflect;
import lombok.extern.log4j.Log4j2;

import java.util.List;

/**
 * 领域模型的基础Repository基础实现
 *
 * @author xufeng
 * @version 1.0
 * @date 2023-04-06
 */
@Log4j2
public class SysBaseRepository extends BaseRepository {







    public void importEnd(List<IModel> model,ITableInfo tableInfo){

    }

    public Integer importData(IModel model,ITableInfo tableInfo){
        return createOrUpdate(model, tableInfo);
    }
     /**
     * 逻辑删除一个对象
     * @param ids 被逻辑删除的对象
     * @param tableInfo 数据表结构
     *
     * @return
     */
    public Integer batchLogicalDelet(List<String> ids, ITableInfo tableInfo,IUserCredentials userCredentials) throws DalFrameException {
        if(ids == null || ids.size() == 0){
            throw new DalFrameException(ErrorCode.ARGS_ERROR,"ids参数不能为空");
        }
        if(tableInfo == null){
            throw new DalFrameException(ErrorCode.ARGS_ERROR,"tableInfo参数不能为空");
        }
        IColumnMapping columnMappingPk = tableInfo.findPkFirst();
        if(columnMappingPk == null){
            throw new DalFrameException(ErrorCode.ARGS_ERROR,"model必须设置主键");
        }
        SetWrapCollection setWrapCollection = new SetWrapCollection();
        ConditionValueCollection whereCollection = new ConditionValueCollection();
        setWrapCollection.addAutoincrement(BaseModel.VERSION_NAME).
                add(BaseModel.UPDATETIME_NAME).
                add(BaseModel.UPDATEBY_NAME,userCredentials.getName()).
                add(BaseModel.DELFLAG_NAME,BaseModel.DEL_FALG_Y);
        for(String id : ids) {
            whereCollection.add(new ConditionValue(columnMappingPk.getFieldName(), Operator.EQ, id,"X"));
        }
        return defDataBaseAccess.updateMany(tableInfo,setWrapCollection,whereCollection);
    }

    /**
     * 逻辑删除一个对象
     * @param model 被逻辑删除的对象
     * @param tableInfo 数据表结构
     *
     * @return
     */
    public Integer logicalDelet(IModel model, UpdateWhereMode updateWhereMode,ITableInfo tableInfo,IUserCredentials userCredentials){

        UpdateAttr updateAttr = new UpdateAttr(updateWhereMode);
        updateAttr.addfieldName(BaseModel.UPDATETIME_NAME).addfieldName(BaseModel.UPDATEBY_NAME).addfieldName(BaseModel.DELFLAG_NAME);
        XReflect.setFieldValue(model,BaseModel.DELFLAG_NAME,BaseModel.DEL_FALG_Y);
        XReflect.setFieldValue(model,BaseModel.UPDATEBY_NAME,userCredentials.getName());
        return defDataBaseAccess.update(model,tableInfo, updateAttr,false);

    }



    /**
     * 逻辑删除一个对象
     * @param ids 被逻辑删除的对象
     * @param tableInfo 数据表结构
     *
     * @return
     */
    public Integer batchEnableModel(List<String> ids, ITableInfo tableInfo,IUserCredentials userCredentials) throws DalFrameException {
        if(ids == null || ids.size() == 0){
            throw new DalFrameException(ErrorCode.ARGS_ERROR,"ids参数不能为空");
        }
        if(tableInfo == null){
            throw new DalFrameException(ErrorCode.ARGS_ERROR,"tableInfo参数不能为空");
        }
        IColumnMapping columnMappingPk = tableInfo.findPkFirst();
        if(columnMappingPk == null){
            throw new DalFrameException(ErrorCode.ARGS_ERROR,"model必须设置主键");
        }
        SetWrapCollection setWrapCollection = new SetWrapCollection();
        ConditionValueCollection whereCollection = new ConditionValueCollection();
        setWrapCollection.addAutoincrement(BaseModel.VERSION_NAME).
                add(BaseModel.UPDATETIME_NAME).
                add(BaseModel.UPDATEBY_NAME,userCredentials.getName()).
                add(BaseModel.DELFLAG_NAME,BaseModel.DEL_FALG_N);
        for(String id : ids) {
            whereCollection.add(new ConditionValue(columnMappingPk.getFieldName(), Operator.EQ, id,"X"));
        }
        return defDataBaseAccess.updateMany(tableInfo,setWrapCollection,whereCollection);
    }


    /**
     * 逻辑删除一个对象
     * @param model 被逻辑删除的对象
     * @param tableInfo 数据表结构
     *
     * @return
     */
    public Integer enableModel(IModel model, UpdateWhereMode updateWhereMode,ITableInfo tableInfo,IUserCredentials userCredentials){

        UpdateAttr updateAttr = new UpdateAttr(updateWhereMode);
        updateAttr.addfieldName(BaseModel.UPDATETIME_NAME).addfieldName(BaseModel.UPDATEBY_NAME).addfieldName(BaseModel.DELFLAG_NAME);
        XReflect.setFieldValue(model,BaseModel.DELFLAG_NAME,BaseModel.DEL_FALG_N);
        XReflect.setFieldValue(model,BaseModel.UPDATEBY_NAME,userCredentials.getName());
        return defDataBaseAccess.update(model,tableInfo, updateAttr,false);

    }
    public void setBaseAttrVal(IModel model,IUserCredentials userCredentials){
        if(model instanceof BaseModel){
            BaseModel baseModel = (BaseModel)model;
            baseModel.setBaseAttrVal(userCredentials);
        }
        if(model instanceof XDynamicModel){
            XDynamicModel xDynamicModel = (XDynamicModel)model;
            xDynamicModel.set(BaseModel.CREATEBY_NAME,userCredentials.getName());
            xDynamicModel.set(BaseModel.UPDATEBY_NAME,userCredentials.getName());
        }
    }

    public void setBaseAttrVal(IModel model,String userName){
        if(model instanceof BaseModel){
            BaseModel baseModel = (BaseModel)model;
            if(StringUtil.isNotEmpty(userName)){
//                baseModel.setCreateBy(userName);
//                baseModel.setUpdateBy(userName);
            }
        }
        if(model instanceof XDynamicModel){
            XDynamicModel xDynamicModel = (XDynamicModel)model;
            xDynamicModel.set(BaseModel.CREATEBY_NAME,userName);
            xDynamicModel.set(BaseModel.UPDATEBY_NAME,userName);
        }
    }

    public void setBaseAttrVal(List<IModel> modelList, IUserCredentials userCredentials){

        if(modelList != null && modelList.size() > 0){
            for(IModel model : modelList){
                this.setBaseAttrVal(model,userCredentials);
            }
        }

    }
}
